package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.inter.LoginCallBack;
import com.example.util.RegisterStudent;

public class SiginActivity extends AppCompatActivity implements LoginCallBack, View.OnClickListener {
    private EditText studentName, studentNumber, studentEmail, studentPassword;
    private ProgressBar progressBar;
    private RegisterStudent registerStudent;
    private LinearLayout layout_form,layout_bottom;
    private Button btn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sigin);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initView();
        btn_register.setOnClickListener(this);
        registerStudent = new RegisterStudent();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(SiginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initView() {
        studentName = (EditText) findViewById(R.id.student_name);
        studentEmail = (EditText) findViewById(R.id.student_email);
        studentNumber = (EditText) findViewById(R.id.student_number);
        studentPassword = (EditText) findViewById(R.id.student_password);
        progressBar = (ProgressBar) findViewById(R.id.login_progress);
        layout_form = (LinearLayout) findViewById(R.id.layout_form);
        btn_register = (Button) findViewById(R.id.email_resigter_in_button);
        layout_bottom = (LinearLayout) findViewById(R.id.layout_bottom);

    }

    @Override
    public void onSuccess(String info) {
        Snackbar.make(layout_bottom,"success",Snackbar.LENGTH_SHORT).show();
        layout_form.setAlpha(0);
        progressBar.setVisibility(View.GONE);
        layout_form.setVisibility(View.GONE);
        Log.i("res",info.toString());

    }

    @Override
    public void onFailed() {
        Snackbar.make(layout_bottom,"fail",Snackbar.LENGTH_SHORT).show();
        layout_form.setAlpha(0);
        progressBar.setVisibility(View.GONE);
        layout_form.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.email_resigter_in_button:
                boolean judevalide = registerStudent.judeValid(this, studentName, studentNumber, studentPassword, studentEmail);
                if (judevalide) {

                } else {
                    registerStudent.sendSignInfo(getViewString(studentName),
                            getViewString(studentNumber),
                            getViewString(studentEmail),
                            getViewString(studentPassword),
                            this);
                    progressBar.setVisibility(View.VISIBLE);
                    layout_form.setAlpha(0.4f);
                    layout_form.setVisibility(View.VISIBLE);
                }

        }
    }

    private String getViewString(EditText view) {
        return view.getText().toString().trim();
    }
}
